package com.dengyuanke.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dengyuanke.upms.dto.TreeNode;
import com.dengyuanke.upms.entity.Menu;
import com.dengyuanke.upms.entity.RoleMenu;
import com.dengyuanke.upms.entity.UserRole;
import com.dengyuanke.upms.mapper.MenuMapper;
import com.dengyuanke.upms.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dengyuanke.upms.service.IRoleMenuService;
import com.dengyuanke.upms.service.IRoleService;
import com.dengyuanke.upms.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author dengyuanke
 * @since 2019-06-29
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private IRoleMenuService roleMenuService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IUserRoleService userRoleService;

    @Override
    public List<TreeNode> findMenusByUserId(String userId) {
        List<UserRole> list = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userId));

        return null;
    }

    @Override
    public List<Menu> findMenuByRoleId(Long roleId) {
        List<Long> menuIds = roleMenuService.list(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, roleId))
                .stream()
                .map(RoleMenu::getMenuId)
                .collect(Collectors.toList());
        List<Menu> menus = this.list(new LambdaQueryWrapper<Menu>().in(Menu::getId, menuIds));
        return menus;
    }
}
